package rescsa.gob.pe.requerimiento.action;

import java.util.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;


import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.opensymphony.xwork2.ActionSupport;

public class RequerimientoAction extends ActionSupport implements
		ServletRequestAware {

	private static final long serialVersionUID = 1L;

	private List<File> userImage=new ArrayList<File>();
	private List<String> userImageContentType=new ArrayList<String>();
	private List<String> userImageFileName=new ArrayList<String>();

	private HttpServletRequest servletRequest;
	private static final String UPLOADS_DIRECTORY = "c:/uploads/";
	 int i=0;
	public String upload() throws Exception{
		 try {
			 int i=0;
			   for (File file: userImage) {
			        System.out.println("File :" + file);
//			        String filePath = servletRequest.getSession().getServletContext().getRealPath("/");
//		            System.out.println("Server path:" + filePath);
//		            
//		            File fileToCreate = new File(filePath, this.userImageFileName.get(i));
//		            
//		            FileUtils.copyFile(this.userImage.get(i), fileToCreate);
			        
			        StringBuilder sb = new StringBuilder();
					sb.append(UPLOADS_DIRECTORY);
					sb.append(this.userImageFileName.get(i));
					
					File theFile = new File(sb.toString());
					FileUtils.copyFile(this.userImage.get(i), theFile);
		            i++;
		            
			    }
		 
			    for (String fileName: userImageContentType) {
			        System.out.println("Filename : " + fileName);
			    }
		 
			    for (String fileContentType: userImageFileName) {
			        System.out.println("File type : " + fileContentType);
			    }
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println(""+e.getMessage());
			return INPUT;
		}
	 
 
	    return SUCCESS;
 
	}
 
	public String display() {
		return NONE;
	}
 

	public HttpServletRequest getServletRequest() {
		return servletRequest;
	}


 
    
    public List<File> getUserImage() {
		return userImage;
	}

	public void setUserImage(List<File> userImage) {
		this.userImage = userImage;
	}

	public List<String> getUserImageContentType() {
		return userImageContentType;
	}

	public void setUserImageContentType(List<String> userImageContentType) {
		this.userImageContentType = userImageContentType;
	}

	public List<String> getUserImageFileName() {
		return userImageFileName;
	}

	public void setUserImageFileName(List<String> userImageFileName) {
		this.userImageFileName = userImageFileName;
	}

	public void setServletRequest(HttpServletRequest servletRequest) {
        this.servletRequest = servletRequest;
 
    }

	public String bandejaEntrada() {
		
		return SUCCESS;
	}

	public String nuevaSolicitud() {
		
		return SUCCESS;
	}

	public String consultarSolicitud() {
		System.out.println("c");
		return SUCCESS;
	}

	public String actualizarSolicitud() {
		System.out.println("a");
		return SUCCESS;
	}

	public String verPrototipo() {
		System.out.println("v");
		return SUCCESS;
	}

	public String desplegarRequermiento() {
		System.out.println("v");
		return SUCCESS;
	}
	
}
